package com.zgm.ch05.aop.before._3_5_3;

import org.springframework.aop.framework.ProxyFactory;

public class SecurityExample {

	public static void main(String[] args){
		SecurityManager mgr = new SecurityManager();
		
		SecureBean bean = getSecureBean();
		
		mgr.login("zgm", "****");
		bean.writeSecureMessage();
		mgr.logout();
		
		try{
			mgr.login("abc", "****");
			bean.writeSecureMessage();
		} catch(SecurityException ex){
			ex.printStackTrace();
		} finally {
			mgr.logout();
		}
		
		try{
			bean.writeSecureMessage();
		} catch(SecurityException ex){
			ex.printStackTrace();
		}
		
		mgr.login("zgm", "****");
		bean.writeSecureMessage();
		mgr.logout();
		
	}
	
	public static SecureBean getSecureBean(){
		SecureBean target = new SecureBean();
		SecurityAdvice advice = new SecurityAdvice();
		ProxyFactory factory = new ProxyFactory();
		
		factory.setTarget(target);
		factory.addAdvice(advice);
		
		return (SecureBean) factory.getProxy();
	}
}
